import { SkillBase } from '@/assets/skills/SkillBase.js'
import { GameObject } from '@/assets/GameObject.js'

export class Heal extends SkillBase {
    constructor(player) {
        super(player, 0) // 10秒冷却时间
        this.player = player
        this.skillKey = 'W'
        this.skillName = 'heal'
        this.healAmount = 30

        this.load_skill_icon('https://www.helloimg.com/i/2025/04/11/67f8dfdf1e81b.jpg')
    }

    cast() {
        if (this.player.isDead || this.is_on_cooldown()) {
            return false
        }
        // 回复血量(不超过最大值)
        this.player.info.hp = Math.min(this.player.maxHp, this.player.info.hp + this.healAmount)

        // 创建治疗特效
        this.create_heal_effect()

        // 调用父类方法重置冷却
        super.cast()

        return true
    }

    // 创建治疗特效
    create_heal_effect() {
        // 创建并添加治疗效果实体
        new HealEffectEntity(this.gameMap, {
            x: this.player.info.x,
            y: this.player.info.y,
            radius: this.player.info.radius * 1.5,
            player: this.player,
        })
    }
}

// 治疗效果实体类
class HealEffectEntity extends GameObject {
    constructor(gameMap, info) {
        super()
        this.gameMap = gameMap
        this.info = info
        this.lifespan = 800 // 效果持续时间(毫秒)
        this.particles = []
        this.sparkles = []
        this.lastParticleTime = 0
        this.particleInterval = 30 // 生成粒子的时间间隔（毫秒）

        // 创建初始粒子
        this.createParticles()
    }

    start() {}

    // 创建粒子效果
    createParticles() {
        const particleCount = 15

        // 创建围绕玩家的粒子
        for (let i = 0; i < particleCount; i++) {
            // 随机角度
            const angle = Math.random() * Math.PI * 2
            // 随机距离
            const distance = this.info.radius * (0.2 + Math.random() * 0.8)
            // 计算位置
            const x = this.info.x + Math.cos(angle) * distance
            const y = this.info.y + Math.sin(angle) * distance

            // 随机大小
            const size = this.info.radius * (0.05 + Math.random() * 0.15)

            // 随机上升速度
            const speed = (0.01 + Math.random() * 0.03) * this.info.radius

            // 随机生命周期
            const particleLifespan = 400 + Math.random() * 400

            // 添加粒子
            this.particles.push({
                x: x,
                y: y,
                size: size,
                alpha: 0.2 + Math.random() * 0.5,
                lifespan: particleLifespan,
                maxLifespan: particleLifespan,
                speed: speed,
                angle: angle,
            })
        }

        // 创建闪光效果
        for (let i = 0; i < 5; i++) {
            const angle = Math.random() * Math.PI * 2
            const sparkleLifespan = 200 + Math.random() * 300

            this.sparkles.push({
                angle: angle,
                progress: 0,
                lifespan: sparkleLifespan,
                maxLifespan: sparkleLifespan,
            })
        }
    }

    // 生成新粒子
    generateParticles() {
        const currentTime = Date.now()
        if (currentTime - this.lastParticleTime > this.particleInterval) {
            this.lastParticleTime = currentTime

            // 随机角度
            const angle = Math.random() * Math.PI * 2
            // 随机距离
            const distance = this.info.radius * (0.2 + Math.random() * 0.8)
            // 计算位置
            const x = this.info.x + Math.cos(angle) * distance
            const y = this.info.y + Math.sin(angle) * distance

            // 随机大小
            const size = this.info.radius * (0.05 + Math.random() * 0.15)

            // 随机上升速度
            const speed = (0.01 + Math.random() * 0.03) * this.info.radius

            // 随机生命周期
            const particleLifespan = 400 + Math.random() * 400

            // 添加粒子
            this.particles.push({
                x: x,
                y: y,
                size: size,
                alpha: 0.2 + Math.random() * 0.5,
                lifespan: particleLifespan,
                maxLifespan: particleLifespan,
                speed: speed,
                angle: angle,
            })

            // 限制粒子数量
            if (this.particles.length > 30) {
                this.particles.shift()
            }
        }
    }

    update() {
        // 更新生命周期
        this.lifespan -= this.timeDelta

        // 如果效果已结束，销毁
        if (this.lifespan <= 0) {
            this.destroy()
            return
        }

        // 生成新粒子
        this.generateParticles()

        // 更新所有粒子
        for (let i = this.particles.length - 1; i >= 0; i--) {
            const particle = this.particles[i]

            // 减少粒子生命周期
            particle.lifespan -= this.timeDelta

            // 计算透明度
            particle.alpha = particle.lifespan / particle.maxLifespan

            // 更新粒子位置（缓慢上升并围绕玩家旋转）
            particle.y -= (particle.speed * this.timeDelta) / 16

            // 如果粒子生命周期结束，移除
            if (particle.lifespan <= 0) {
                this.particles.splice(i, 1)
            }
        }

        // 更新闪光效果
        for (let i = this.sparkles.length - 1; i >= 0; i--) {
            const sparkle = this.sparkles[i]
            sparkle.lifespan -= this.timeDelta
            sparkle.progress = 1 - sparkle.lifespan / sparkle.maxLifespan

            if (sparkle.lifespan <= 0) {
                this.sparkles.splice(i, 1)

                // 添加新的闪光
                if (this.lifespan > 300) {
                    const angle = Math.random() * Math.PI * 2
                    const sparkleLifespan = 200 + Math.random() * 300

                    this.sparkles.push({
                        angle: angle,
                        progress: 0,
                        lifespan: sparkleLifespan,
                        maxLifespan: sparkleLifespan,
                    })
                }
            }
        }

        this.draw()
    }

    draw() {
        // 绘制光环效果
        const pulseIntensity = Math.sin(Date.now() * 0.005) * 0.3 + 0.7
        this.gameMap.ctx.beginPath()
        const auraGradient = this.gameMap.ctx.createRadialGradient(
            this.info.x,
            this.info.y,
            0,
            this.info.x,
            this.info.y,
            this.info.radius * pulseIntensity
        )

        const alpha = Math.min(0.5, this.lifespan / 800)
        auraGradient.addColorStop(0, 'rgba(100, 255, 100, ' + alpha * 0.7 + ')')
        auraGradient.addColorStop(0.5, 'rgba(50, 255, 50, ' + alpha * 0.5 + ')')
        auraGradient.addColorStop(1, 'rgba(0, 200, 0, 0)')

        this.gameMap.ctx.fillStyle = auraGradient
        this.gameMap.ctx.arc(
            this.info.x,
            this.info.y,
            this.info.radius * pulseIntensity,
            0,
            Math.PI * 2
        )
        this.gameMap.ctx.fill()

        // 绘制闪光效果
        for (const sparkle of this.sparkles) {
            const distance = this.info.radius * sparkle.progress
            const x = this.info.x + Math.cos(sparkle.angle) * distance
            const y = this.info.y + Math.sin(sparkle.angle) * distance

            // 计算闪光透明度（开始和结束时降低）
            let sparkleAlpha = Math.sin(sparkle.progress * Math.PI) * 0.8

            // 绘制闪光
            this.gameMap.ctx.beginPath()
            const sparkleGradient = this.gameMap.ctx.createRadialGradient(
                x,
                y,
                0,
                x,
                y,
                this.info.radius * 0.15
            )

            sparkleGradient.addColorStop(0, 'rgba(255, 255, 255, ' + sparkleAlpha + ')')
            sparkleGradient.addColorStop(0.6, 'rgba(100, 255, 100, ' + sparkleAlpha * 0.5 + ')')
            sparkleGradient.addColorStop(1, 'rgba(0, 200, 0, 0)')

            this.gameMap.ctx.fillStyle = sparkleGradient
            this.gameMap.ctx.arc(x, y, this.info.radius * 0.15, 0, Math.PI * 2)
            this.gameMap.ctx.fill()
        }

        // 绘制所有粒子
        for (const particle of this.particles) {
            this.gameMap.ctx.beginPath()
            this.gameMap.ctx.globalAlpha = particle.alpha

            // 创建径向渐变
            const gradient = this.gameMap.ctx.createRadialGradient(
                particle.x,
                particle.y,
                0,
                particle.x,
                particle.y,
                particle.size
            )

            gradient.addColorStop(0, 'rgba(255, 255, 255, ' + particle.alpha + ')')
            gradient.addColorStop(0.6, 'rgba(150, 255, 150, ' + particle.alpha * 0.8 + ')')
            gradient.addColorStop(1, 'rgba(50, 200, 50, 0)')

            this.gameMap.ctx.fillStyle = gradient
            this.gameMap.ctx.arc(particle.x, particle.y, particle.size, 0, Math.PI * 2)
            this.gameMap.ctx.fill()
            this.gameMap.ctx.globalAlpha = 1.0
        }

        // 添加十字形发光效果
        this.gameMap.ctx.beginPath()
        this.gameMap.ctx.moveTo(this.info.x - this.info.radius * 0.3, this.info.y)
        this.gameMap.ctx.lineTo(this.info.x + this.info.radius * 0.3, this.info.y)
        this.gameMap.ctx.moveTo(this.info.x, this.info.y - this.info.radius * 0.3)
        this.gameMap.ctx.lineTo(this.info.x, this.info.y + this.info.radius * 0.3)
        this.gameMap.ctx.strokeStyle = 'rgba(100, 255, 100, ' + (this.lifespan / 800) * 0.7 + ')'
        this.gameMap.ctx.lineWidth = this.info.radius * 0.1
        this.gameMap.ctx.stroke()
    }
}
